package com.boot.app.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boot.api.ApiResult;
import com.boot.app.system.domain.SystemMessage;
import com.boot.app.system.dto.MessageDto;
import com.boot.app.system.service.ISystemMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("message")
public class SystemMessageController {

    @Autowired
    private ISystemMessageService messageService;

    @GetMapping("{id}")
    public ApiResult<SystemMessage> getMessage(@PathVariable("id") long id){
        SystemMessage one = messageService.getById(id);
        return ApiResult.success(one);
    }

    @GetMapping("list")
    public ApiResult<List<SystemMessage>> getMessageList(){
        List<SystemMessage> list = messageService.list();
        return ApiResult.success(list);
    }

    @GetMapping("tree")
    public ApiResult<List<MessageDto>> getMessageTree(){
        List<MessageDto> dtoList = messageService.fetchTree();
        return ApiResult.success(dtoList);
    }

    @PostMapping()
    public ApiResult<?> postOrPutMessage(@RequestBody SystemMessage systemMessage){
        messageService.save(systemMessage);
        return ApiResult.success();
    }

    @DeleteMapping("{id}")
    public ApiResult<?> deleteMessage(@PathVariable("id") int id){
        messageService.removeById(id);
        return ApiResult.success();
    }

    @GetMapping("list/{parentId}")
    public ApiResult<List<SystemMessage>> getMessageListByParentId(@PathVariable int parentId){
        LambdaQueryWrapper<SystemMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemMessage::getParentId, parentId);
        List<SystemMessage> list = messageService.list(queryWrapper);
        return ApiResult.success(list);
    }

    @GetMapping("tree/not/leaf")
    public ApiResult<List<MessageDto>> getMessageTreeNotLeaf(){
        List<MessageDto> list = messageService.fetchTreeNotLeaf();
        return ApiResult.success(list);
    }

}
